package com.guxingke.typecho4j.widget.handle.content;

import com.guxingke.typecho4j.Typecho4jException;
import com.guxingke.typecho4j.jfinal.model.Content;
import com.guxingke.typecho4j.jfinal.model.Meta;
import com.guxingke.typecho4j.jfinal.model.Option;
import com.guxingke.typecho4j.route.DispatcherStrategy;
import com.guxingke.typecho4j.route.Route;
import com.guxingke.typecho4j.route.Routes;
import com.guxingke.typecho4j.util.DateUtil;
import com.guxingke.typecho4j.widget.Widget;
import com.guxingke.typecho4j.widget.WidgetHandle;
import com.guxingke.typecho4j.widget.layout.LayoutWidget;
import com.guxingke.typecho4j.widget.layout.front.ArchiveLayoutWidget;
import com.guxingke.typecho4j.widget.layout.front.IndexLayoutWidget;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * 更新编辑配置的处理组件
 * 
 * @author guxingke
 *
 */
public class PostEditHandleWidget extends Widget implements WidgetHandle {
	private String defaultCategory;
	private final String markdownFlag = "<!--markdown-->\n\n";

	@Override
	public boolean init() {
		initLogger();
		defaultCategory = Option.dao.getValue("defaultCategory");
		return true;
	}

	@Override
	public void handle(Controller controller) throws Typecho4jException {

		Long cid = updateContent(controller);

		updateMeta(controller, cid);

		LayoutWidget.refreshLayout(IndexLayoutWidget.class);
		LayoutWidget.refreshLayout(ArchiveLayoutWidget.class);
	}

	private void updateMeta(Controller controller, Long cid) {
		Long mid = null;
		// TODO 暂时不考虑tag的实现
		// String tags = controller.getAttrForStr("tags");
		String[] categories = controller.getParaValues("category[]");
		if (categories == null) {
			mid = Long.parseLong(defaultCategory);
			addRecord(cid, mid);
		} else {
			for (String string : categories) {
				Long temp = Long.parseLong(string);
				addRecord(cid, temp);
			}
		}
	}

	private void addRecord(Long cid, Long mid) {
		Record record = Db
				.findFirst("select * from typecho_relationships where cid = ? and mid = ?",
						cid, mid);
		if (record != null) {
			return;
		}
		record = new Record().set("cid", cid).set("mid", mid);
		Db.save("typecho_relationships", record);
		Meta.dao.increaseCount(mid);
	}

	/**
	 * 更新post表
	 * 
	 * @param controller
	 * @param post
	 * @return
	 */
	private Long updateContent(Controller controller) {
		Content post = controller.getModel(Content.class, "post");
		Integer markdown = controller.getParaToInt("markdown");
		// TODO 暂时不考虑文章的权限
		// String visibility = controller.getPara("visibility");
		if (markdown != null) {
			post.set("text", markdownFlag + post.getStr("text"));
		}
		if (post.get("cid") != null) {
			post.set("modified", DateUtil.getUnixTimestamp());
			post.update();
		} else {
			post.set("created", DateUtil.getUnixTimestamp());
			post.save();
		}

		return post.getLong("cid");
	}

	@Override
	public void configRoute(Routes routes) {
		Route updateEditOptionRoute = new Route("postEdit",
				this.getClass(), "/admin/manage-posts",
				DispatcherStrategy.REDIRECT);
		routes.addRoute(updateEditOptionRoute);
	}
}
